package com.mm.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HystrixController {
    private static Logger logger = LoggerFactory.getLogger(HystrixController.class);

    @HystrixCommand(fallbackMethod = "testHystrixFallback")
    @GetMapping(value = "/testHystrix")
    public String testHystrix() {
        throw new RuntimeException("testHystrix_throw_exception");
    }

    public String testHystrixFallback(){
        return "/testHystrix fallback";
    }

    //在方法上写时省略hystrix.command.default.
    @HystrixCommand(commandKey = "testHystrixTimeout",
        commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")
        }
    )
    @GetMapping(value = "/testHystrixTimeout")
    public String testHystrixTimeout() throws InterruptedException {
        Thread.sleep(1200L);
        return "testHystrixTimeout访问成功";
    }
}

